<?php

namespace app\admin\dao;

use app\common\model\LawCase;
use ShidianUtil\Thinkphp\Dao\DaoTrait;

/**
 * LawCaseDao 数据访问对象
 * Class LawCaseDao
 * @package app\dao
 * @method LawCase findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method LawCase insertActive(array $params, array $allow_field = [])
 * @method LawCase updateActive(LawCase $model, array $params, array $allow_field = [])
 * @method LawCase findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method LawCase updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(LawCase $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class LawCaseDao
{
    use DaoTrait;

    /**
     * @var \app\common\model\LawCase
     */
    private $baseModel;

    /**
     * LawCaseDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = LawCase::class;
    }

    /**
     * 待售案源列表
     * @param $categoryId
     * @param $provinceId
     * @param $cityId
     * @param $startTime
     * @param $endTime
     * @param $kw
     * @param int|null $page
     * @param int|null $page_size
     * @return array
     */
    public function listLawCases($isPublish, $categoryId, $provinceId, $cityId, $startTime, $endTime, $kw, ?int $page, ?int $page_size): array
    {
        $query = LawCase::when(is_numeric($isPublish), fn($query) => $query->where('isPublish', $isPublish))
            ->where(fn($query) => $query->where('status', '<>', 2))
            ->when(is_id($categoryId), fn($query) => $query->where('categoryId', $categoryId))
            ->when(is_id($provinceId), fn($query) => $query->where('provinceId', $provinceId))
            ->when(is_id($cityId), fn($query) => $query->where('cityId', $cityId))
            ->when(is_numeric($startTime), fn($query) => $query->where('created', '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where('created', '<=', $endTime))
            ->when(not_blank($kw), fn($query) => $query->search('uid|ask', $kw))
            ->with([
                'category' => fn($query) => $query->field(['id', 'name']),
                'province' => fn($query) => $query->shortArea(),
                'city' => fn($query) => $query->shortArea(),
                'member' => fn($query) => $query->shortMember(),
                'creator' => fn($query) => $query->shortMember()->field(['phone']),
                'money',
                'days',
            ])
            ->order('id', 'DESC')
            ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }

    /**
     * 待售案源列表统计
     * @param $isPublish
     * @param $categoryId
     * @param $provinceId
     * @param $cityId
     * @param $startTime
     * @param $endTime
     * @param $kw
     * @return array
     */
    public function statisticsListLawCases($isPublish, $categoryId, $provinceId, $cityId, $startTime, $endTime, $kw): array
    {
        $query = LawCase::when(is_numeric($isPublish), fn($query) => $query->where('isPublish', $isPublish))
            ->where(fn($query) => $query->where('status', '<>', 2))
            ->when(is_id($categoryId), fn($query) => $query->where('categoryId', $categoryId))
            ->when(is_id($provinceId), fn($query) => $query->where('provinceId', $provinceId))
            ->when(is_id($cityId), fn($query) => $query->where('cityId', $cityId))
            ->when(is_numeric($startTime), fn($query) => $query->where('created', '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where('created', '<=', $endTime))
            ->when(not_blank($kw), fn($query) => $query->search('uid|ask', $kw));

        return [
            'new_count' => $query->count(),
            'unset_count' => (clone $query)->where('isPublish', '0')->count(),
            'set_money_sum' => (clone $query)->where('isPublish', '1')->sum('totalPrice'),
        ];
    }
}
